/**
 * Copyright (c) 2018, Alex. All rights reserved.
 */
package com.oschina.bottle.convert.loader;

import com.oschina.bottle.convert.Convert;
import com.oschina.bottle.convert.Converter;
import com.oschina.bottle.convert.loader.scanner.ConverterClassPathScanner;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * 加载类
 *
 * @author <a href="mailto:zhongchao@gegejia.com">zhong</a>
 * @version 1.0 2018/1/20
 * @since 1.0
 */
@Component
public class ConverterLoader implements ApplicationListener<ContextRefreshedEvent> {
    @Value("${bean.converter.basePackage:com.ggj,com.gegejia}")
    private String scanBasePackage;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event)
    {
        ConverterClassPathScanner scanner = new ConverterClassPathScanner(this.getClass().getClassLoader(), this.scanBasePackage.split(","));

        List<Converter> beanConverters = scanner.doScan();

        beanConverters.forEach(Convert::registry);
    }
}
